<?php
namespace Controllers;
use Models\CategoryModel;

class CommonController {
    public function response($tpl, $data = []) {
        $file = './Views/' . $tpl . '.tpl.php';
        if (!file_exists($file)) exit('模板文件' . $file . '不存在');
        $cateModel = new CategoryModel();
        $navLists = $cateModel->getAllCategories();
        extract($data); // $message = '欢迎使用19FrameWork框架!!!'
        // $userList = [[], []]
        require_once $file;
    }

    public function responseJson($status, $message, $data=[]) {
        $data = [
            'status' => $status,
            'message' => $message,
            'data' => $data
        ];
        return json_encode($data);
    }

    public function loginJump() {
        if ($this->isLogin()) {
            echo '<script>window.location.href="/";</script>'; exit;
        }
    }

    public function noLoginJump() {
        if (!$this->isLogin()) {
            echo '<script>window.location.href="/index.php?c=user&a=login";</script>'; exit;
        }
    }

    public function isLogin() {
        if (isset($_SESSION['user_id']) && isset($_SESSION['username'])) {
            return true;
        } else {
            return false;
        }
    }
}